//
//  NSArray+RunTime.m
//  测试数组越界
//
//  Created by 曹运 on 16/7/11.
//  Copyright © 2016年 曹运. All rights reserved.
//

#import "NSArray+RunTime.h"

@implementation NSArray (RunTime)
+ (void)load{
    
    SEL safeSel   = @selector(safeObjectAtIndex:);
    SEL unsafeSel = @selector(objectAtIndex:);
    
    Class class = NSClassFromString(@"__NSArrayI");
    
    Method safeMethod   = class_getInstanceMethod(class, safeSel);
    Method unsafeMethod = class_getInstanceMethod(class, unsafeSel);
    method_exchangeImplementations(unsafeMethod, safeMethod);
    
   
}

- (id)safeObjectAtIndex:(NSUInteger)index {
    if (self == nil) {
        return nil;
    }
    if (self.count == 0) {
        return nil;
    }
    if (index > (self.count - 1)) {
//        NSAssert(NO, @"beyond the boundary");
        return nil;
    } else {
        return [self safeObjectAtIndex:index];
    }
}

@end
